<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 主题
 */
class Broadcast extends Api{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function index(){
        $id = $this->auth->id;
        $per_page = $this->request->param('per_page',10);
        $page = $this->request->param('page',1);
        $total = Db::name('BroadcastUser')->alias('u')->join('__BROADCAST__ b','b.id = u.broadcast_id')->where('u.user_id',$id)->count();
        $list = Db::name('BroadcastUser')->alias('u')->join('__BROADCAST__ b','b.id = u.broadcast_id')->where('u.user_id',$id)->order('b.id desc')->page($page,$per_page)->field('b.id,b.title,b.image,b.date,address')->select();
        if(!empty($list)){
            foreach ($list as $key => &$value) {
                $value['image'] = cdnurl($value['image'])."?x-oss-process=image/resize,w_750";
                $value['date'] = date('Y-m-d H:i:s',$value['date']);
            }
        }else{
            $list = [];
        }
        $this->success('主题', ['total'=>$total,'list'=>$list]);
    }
    public function info(){
        $user_id = $this->auth->id;
        $id = $this->request->param('id');
        $detail = Db::name('Broadcast')->where('id',$id)->field('id,title,image,zan,browse,cover,auto_video,image_source')->find();
        if(!empty($detail)){
            $buser = Db::name('BroadcastUser')->where('user_id',$user_id)->where('broadcast_id',$id)->find();
            if(empty($buser)){
                Db::name('BroadcastUser')->insert(['user_id'=>$user_id, 'broadcast_id'=>$id]);
            }
            $detail['image'] = cdnurl($detail['image'])."?x-oss-process=image/resize,w_750";
            $detail['cover'] = cdnurl($detail['cover'])."?x-oss-process=image/resize,w_750";
           
        }
        $this->success('主题详情', $detail);
    }
    public function detail(){
        $user_id = $this->auth->id;
        $id = $this->request->param('id');
        $detail = Db::name('Broadcast')->where('id',$id)->field('id,title,image,zan,browse,cover,auto_video,image_source,is_kf')->find();
        $image = Db::name('BroadcastImage')->where('broadcast_id',$id)->order('weigh asc')->field('id,title')->select();
        if(!empty($image)){
            foreach ($image as $key => &$value) {
                $value['title']= date('n.d',strtotime($value['title']));
            }
            array_unshift($image,[
                'id'=>0,
                'title'=>'全部',
            ]);
        }else{
            $image =[   [
                'id'=>0,
                'title'=>'全部',
            ]];
        }
        if(!empty($detail)){
            $buser = Db::name('BroadcastUser')->where('user_id',$user_id)->where('broadcast_id',$id)->find();
            if(empty($buser)){
                Db::name('BroadcastUser')->insert(['user_id'=>$user_id, 'broadcast_id'=>$id]);
            }
            $detail['image'] = cdnurl($detail['image'])."?x-oss-process=image/resize,w_750";
            $detail['cover'] = cdnurl($detail['cover'])."?x-oss-process=image/resize,w_750";
            
            $detail['browse'] = $detail['browse']+1;
            Db::name('Broadcast')->where('id',$id)->setInc('browse');
            $zan = Db::name('BroadcastZan')->where('user_id',$user_id)->where('broadcast_id',$id)->find();
            if(!empty($zan)){
                $detail['is_zan'] = 1;
            }else{
                $detail['is_zan'] = 0;
            }
            $detail['photo_num'] =  Db::name('BroadcastImage')->where('broadcast_id',$id)->sum('photo_num');
            $detail['browse'] =  Db::name('BroadcastUser')->where('broadcast_id',$id)->count()?:0;
            $detail['photo_cate']=$image;
        }
        $this->success('主题详情', $detail);
    }

    public function zan(){
        $user_id = $this->auth->id;
        $id = $this->request->param('id');
        Db::name('Broadcast')->setInc('zan');
        Db::name('BroadcastZan')->insert(array('user_id'=>$user_id, 'broadcast_id'=>$id));
        $this->success('点赞成功');
    }
    public function images(){
        $user_id = $this->auth->id;
        $id = $this->request->param('broadcast_id');
        $cate_id = $this->request->param('id',0);
        $page = $this->request->param('page',1);
        $pagesize = $this->request->param('pagesize',10);
        $map['broadcast_id'] = $id;
        $map['status'] = 1;
        $map['is_shift'] = 1;
        $map['is_show'] = 1;
        if($cate_id >0){
            $map['broadcast_image_id'] = $cate_id;
        }
        $width = Db::name('Broadcast')->where('id',$id)->value('width');
        $total =  Db::name('BroadcastImagePhoto')->where($map)->count();
        $image = Db::name('BroadcastImagePhoto')->where($map)->order('broadcast_image_id asc')->field('id,image,is_download')->page($page,$pagesize)->select();
        if(!empty($image)){
            foreach ($image as $key => &$value) {  
                $value['source'] =cdnurl($value['image']);
                $value['image'] = cdnurl($value['image'])."?x-oss-process=image/resize,w_".$width.",h_".$width.",m_mfit/quality,q_80";
            }
        }else{
            $image = [];
        }
        $this->success('主题图片列表', ['total'=>$total,'list'=>$image]);
    }
    public function video(){
        $user_id = $this->auth->id;
        $id = $this->request->param('id');
        $page = $this->request->param('page',1);
        $pagesize = $this->request->param('pagesize',10);
        $total = Db::name('BroadcastVedio')->where('broadcast_id',$id)->count();
        $video = Db::name('BroadcastVedio')->where('broadcast_id',$id)->order('weigh desc')->field('id,vedio_title as title,vedio')->page($page,$pagesize)->select();
        $data = [];
        if(!empty($video)){
            foreach ($video as $key => $value) {
                $data[$key]['title'] =$value['title'];
                $data[$key]['url'] =cdnurl($value['vedio']);
            }
        }
        $this->success('主题视频列表',['total'=>$total,'list'=>$data]);
    }
    public function moment(){
        $id = $this->request->param('broadcast_id');
        $page = $this->request->param('page',1);
        $pagesize = $this->request->param('pagesize',10);
        $map['status'] = 1;
        $map['is_moment'] = 1;
        $is_moment = Db::name('Broadcast')->where('id',$id)->value('is_moment');
        $width = Db::name('Broadcast')->where('id',$id)->value('width');
        if(!empty($moment)){
            $images =  explode(',',$moment);
        }
        if($is_moment == 1){
            $map['broadcast_id'] = ['in',['0',$id]];
        }else{
            $map['broadcast_id'] = $id;
        }
        $total =  Db::name('BroadcastImagePhoto')->where($map)->count();
        $image = Db::name('BroadcastImagePhoto')->where($map)->order('id desc')->field('id,image,moment')->page($page,$pagesize)->select();

        if(!empty($image)){
            foreach ($image as $key => &$value) {  
                $value['source'] =cdnurl($value['image'])."?x-oss-process=image/resize,w_".$width.",h_".$width.",m_mfit/quality,q_80";
                $value['moment'] = cdnurl($value['moment'])."?x-oss-process=image/resize,w_".$width.",h_".$width.",m_mfit/quality,q_80";
            }
        }else{
            $image = [];
        }
        $this->success('精彩瞬间列表', ['total'=>$total,'list'=>$image]);
    }
    public function photo(){
        $id = $this->request->param('photo_id');
        $photo = Db::name('BroadcastImagePhoto')->where('id',$id)->find();
        if(!empty($photo)){
            $domain = $this->request->domain();
            $photo['image'] = cdnurl($photo['image'])."?x-oss-process=image/resize,w_750,h_750,m_mfit/quality,q_80";
            $avatar = Db::name('BroadcastImageAvatar')->where('status',1)->where('photo_id',$photo['id'])->field('avatar,id as avatar_id')->select();
            if(!empty($avatar)){
                foreach ($avatar as $key => &$value) {
                    $value['avatar']= $domain.$value['avatar'];
                }
            }else{
                $avatar = [];
            }

            $photo['avatar']=$avatar;
        }else{
            $photo=[];
        }
        $this->success('主题图片关联头像', $photo);
    }

    public function avatar(){
        $page = $this->request->param('page',1);
        $pagesize = $this->request->param('pagesize',10);
        $avatar_id = $this->request->param('avatar_id','');
        if(empty($avatar_id)){
            $this->error('头像ID不能为空');
        }
        $domain = $this->request->domain();
        $avatar = Db::name('BroadcastImageAvatar')->where('id',$avatar_id)->field('id,avatar,avatar_id,photo_id')->find();
        $avatar['avatar'] = $domain.$avatar['avatar'];
        $photo = Db::name('BroadcastImagePhoto')->where('id',$avatar['photo_id'])->find();
        $broadcast = Db::name('Broadcast')->where('id',$photo['broadcast_id'])->find();
        $avatar['broadcast_id'] = $broadcast['id'];
        $avatar['broadcast_title'] = $broadcast['title'];
        $avatar['broadcast_image'] = cdnurl($broadcast['image'])."?x-oss-process=image/resize,w_750,h_750,m_mfit/quality,q_80";

        $total = Db::name('BroadcastImageAvatar')->alias('a')->join('__BROADCAST_IMAGE_PHOTO__ p','p.id = a.photo_id')->where('p.is_shift',1)->
        where('p.status',1)->where('a.avatar_id',$avatar['avatar_id'])->where('p.broadcast_id',$broadcast['id'])->count();
        $photo = Db::name('BroadcastImageAvatar')->alias('a')->join('__BROADCAST_IMAGE_PHOTO__ p','p.id = a.photo_id')->where('p.is_shift',1)->
        where('p.status',1)->where('a.avatar_id',$avatar['avatar_id'])->where('p.broadcast_id',$broadcast['id'])->field('p.id,p.image')->select();
        if(!empty($photo)){
            foreach ($photo as $key => &$value) {
                $value['image'] = cdnurl($value['image'])."?x-oss-process=image/resize,w_750,h_750,m_mfit/quality,q_80";
            }
        }else{
            $photo=[];
        }

        $this->success('AI识图分页列表',['avatar'=>$avatar,'total'=>$total,'list'=>$photo]);
    }

}